/*
 * @Description: Description
 * @Author: Kerwin
 * @Date: 2025-05-06 15:34:11
 * @LastEditTime: 2025-06-03 11:39:39
 * @LastEditors:  Please set LastEditors
 */
/*
 * @Description: 大牌购物车
 * @Author: Kerwin
 * @Date: 2025-05-06 15:34:11
 * @LastEditTime: 2025-05-13 14:40:37
 * @LastEditors:  Please set LastEditors
 */

import { log } from 'node:console'
import { defineStore } from 'pinia'

const goodStore = defineStore('goodStore', {
  state: () => {
    return {
      goodsItem: {},
      goodsList: [] as any[],
      goodNum: 1,
      shopInfo: {},
      orderId: '',
      isApp: false,
      brandId: 1,
      shopData: {}
    }
  },

  actions: {
    // brandId
    setBrandId(data: any) {
      this.brandId = data
    },
    setShopData(data: any) {
      this.shopData = data
    },
    // 是否是app
    setIsApp(data: any) {
      this.isApp = data
    },

    // 是否是app
    clearnIsApp() {
      this.isApp = false
    },

    // 餐厅信息
    setShopInfo(data: any) {
      this.shopInfo = data
    },
    //选中的单个商品
    setGoodsItem(data: any) {
      this.goodsItem = data
    },
    setOrderId(data: any) {
      this.orderId = data
    },
    //添加商品
    addGoods(data: any) {
      //data加一个时间戳
      data.time = Date.now()
      const index = this.goodsList.findIndex((item: any) => {
        switch (data.shopBrandId) {
          case '13': // 瑞幸
            if (!item.details?.otherStr || !data.details?.otherStr) {
              // 其中一方没有 otherStr → 只比较 shopProductSkuId
              return item.shopProductSkuId === data.shopProductSkuId
            } else {
              // 双方都有 otherStr → 比较 shopProductSkuId + otherStr
              return (
                item.shopProductSkuId === data.shopProductSkuId &&
                item.details.otherStr === data.details.otherStr
              )
            }
          case '1': // KFC
          default:
            return item.shopProductSkuId === data.shopProductSkuId
        }
      })

      if (index !== -1) {
        // 存在 → 修改商品数量
        console.log(index, '存在则替换', this.goodsList[index].num)
        //  debugger
        const updatedItem = {
          ...this.goodsList[index],
          num: this.goodsList[index].num + data.num
        }
        this.goodsList.splice(index, 1, updatedItem)
      } else {
        // 不存在 → 添加新商品
        this.goodsList.push(data)
      }

      console.log(this.goodsList, '888', index)
    },
    //清空商品
    clearGoods() {
      this.goodsList = []
      this.goodNum = 0
      this.orderId = ''
      // this.shopInfo = {}
      this.orderId = ''
      //清除单个数据
      sessionStorage.removeItem('wzLocation')
      // uni.clearStorageSync()
    },

    //减去商品
    reduceGoods(data: any) {
      // 假设商品的唯一标识是 data.id
      const index = this.goodsList.findIndex((item: any) => {
        switch (data.shopBrandId) {
          case '13': // 瑞幸
            if (!item.details?.otherStr || !data.details?.otherStr) {
              // 其中一方没有 otherStr → 只比较 shopProductSkuId
              return item.shopProductSkuId === data.shopProductSkuId
            } else {
              // 双方都有 otherStr → 比较 shopProductSkuId + otherStr
              return (
                item.shopProductSkuId === data.shopProductSkuId &&
                item.details.otherStr === data.details.otherStr
              )
            }
          case '1': // KFC
          default:
            return item.shopProductSkuId === data.shopProductSkuId
        }
      })

      if (index !== -1) {
        console.log(1)
        // 减去目标商品
        this.goodsList.splice(index, 1)
      }
    },

    changeGoodNum(num: number) {
      this.goodNum = num
    }
  },
  persist: {
    storage: sessionStorage // 改为 sessionStorage
  }
})

export default goodStore
